const jwt = require("jsonwebtoken");
const { PRIVATE_KEY } = require("../config/secret");
class LoginController {
    sign(ctx, next) {
        // 1.获取用户信息
        const { user: { id, name } = {} } = ctx;
        // 2.颁发令牌token
        const payload = { id, name };
        const token = jwt.sign(payload, PRIVATE_KEY, {
            expiresIn: 24 * 60 * 60,
            algorithm: "RS256",
        });
        // 3.颁发令牌传入token,返回用户信息
        ctx.body = {
            code: 0,
            data: {
                token,
                id,
                name,
            },
        };
    }

    test(ctx, next) {
        /* // 1.获取token
        const authorization = ctx.headers.authorization;
        const token = authorization.replace("Bearer ", "");
        // 2.验证token
        try {
            const result = jwt.verify(token, PUBLIC_KEY, {
                algorithms: ["RS256"],
            });
            console.log("result", result);
            ctx.body = `可以login/test接口~`;
        } catch (error) {
            ctx.app.emit("error", UNAUTHORIZATION, ctx);
        } */

        ctx.body = `可以login/test接口~`;
    }
}

module.exports = new LoginController();
